import { Component, OnInit } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { News } from '../../providers/news-data/news';
import { User } from '../../providers/login-data/user';
import { Storage } from '@ionic/storage';
import { CommentInfo } from '../../providers/comment-data/comment';
import { CommentDataProvider } from '../../providers/comment-data/comment-data';



@Component({
  selector: 'page-news-comment',
  templateUrl: 'news-comment.html',
})
export class NewsCommentPage implements OnInit {


  news:News;
  user:User;
  commentContent:string;

  constructor(public navCtrl: NavController, 
    public navParams: NavParams,
    public storage:Storage,
    public commentDataProvider: CommentDataProvider) {
  }


  ngOnInit(): void {
    //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
    //Add 'implements OnInit' to the class.
    this.news= this.navParams.get('news');
  }

  ionViewDidLoad() {
    this.storage.get('LOGIN_INFO').then(val=>{
      this.user = JSON.parse(val);
    });
  }


  postComment(){
    if(this.commentContent.length > 0){
      let _comment: CommentInfo={
        content: this.commentContent,
        userId: this.user.objectId,
        newsId: this.news.objectId
      };
      this.commentDataProvider.addComment(_comment).toPromise().then(()=>{
        this.navCtrl.pop();
      });
    }
  }




}
